<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">添加曲目
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="musicInfoList">
      <el-table-column label="名称" align="center" prop="musicName" />
      <el-table-column label="顺序" align="center" prop="musicIndex" sortable="">
        <template slot-scope="scope">
          {{ scope.row.musicIndex?scope.row.musicIndex:"0" }}
        </template>
      </el-table-column>
      <!--      <el-table-column label="封面" align="center" prop="imgUrl" />-->
      <el-table-column label="封面" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50" />
          <!-- <img :src="scope.row.imgUrl" alt=""> -->
        </template>
      </el-table-column>
      <el-table-column label="音乐家" align="center" prop="musicMaker" />
      <el-table-column label="时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" 
            @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="removeMusic(scope.row)">移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改 -->
    <el-dialog :title='title' :visible.sync="opens" width="50%" :before-close="handleClose">

      <!-- <span>这是一段信息</span> -->
      <el-form :model="queryParams" ref="queryParams" size="small" :inline="true" label-width="100px">
        <el-form-item label="名称" prop="musicName">
          <el-input v-model="queryParams.musicName" clearable />
        </el-form-item>
        <el-form-item label="顺序" prop="musicIndex">
          <el-input v-model="queryParams.musicIndex" clearable />
        </el-form-item>
        <el-form-item label="音乐家" prop="musicMaker">
          <el-input v-model="queryParams.musicMaker" clearable />
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="800px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="名称" prop="musicName">
          <el-input v-model="queryParams.musicName" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="musicChooseList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" />
        <el-table-column label="名称" align="center" prop="musicName" />
        <!--      <el-table-column label="封面" align="center" prop="imgUrl" />-->
        <el-table-column label="封面" align="center" prop="imgUrl">
          <template slot-scope="scope">
            <!-- <image-preview :src="scope.row.imgUrl"  /> -->
            <image-preview :src="scope.row.imgUrl" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="时间" align="center" prop="operatorTime" />
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
      <el-divider></el-divider>
      <el-button type="primary" @click="addToBookAction">添加到书籍</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { getMusics } from "@/api/system/bookinfo";
import { listMusicinfo,updateMusicinfo,getMusicinfo } from "@/api/system/musicinfo";
import { addMusic, removeMusic } from "../../../api/system/bookinfo";

export default {
  name: "bookMusic",
  data() {
    return {
      bookId: null,
      musicInfoList: [],
      // 遮罩层
      loading: true,
      musicChooseList: [],

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        musicName: null,
        musicIndex:null,
        musicMaker:null,
        id:null,
      },
      
      dialogVisible: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      opens: false,
      open:false,
      total: 0,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      title:"",
      form:{},
       // 表单校验
       rules: {
      }
    }
  },
  created() {
    const bookId = this.$route.params && this.$route.params.bookId;
    this.bookId = bookId;
    this.getMusicList(bookId)
  },
  methods: {
     // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false;
      this.reset();
    },
  
   // 表单重置
   reset() {
      this.form = {
        id: null,
        musicName: null,
        musicMaker: null,
        musicIndex: null,
        rank: null,
        img: null,
        makeTime: null,
        musicDesc: null,
        styleLabel: null,
        pdfContent: null,
        onlyVipShow: null,
        onlyVipDown: null,
        onlyTester: null,
        operatorUser: null,
        operatorTime: null,
        musicWav: null,
        horizontalJson: null,
        verticalJson: null,
        musicForSdk: null,
        horizontalImg: null,
        verticalImg: null,
        status: null
      };},
     /** 修改按钮操作 */
     handleUpdate(row) {
      this.reset();
      // 显示修改弹窗
      // this.dialogVisible = true
      this.opens = true;

      const id = row.id || this.ids
      getMusicinfo(id).then(response => {
        this.form = response.data;
        this.opens = true;
        this.title = "修改乐曲信息";
      })
      // 获取当前行的musicName、musicIndex、musicMaker值
      this.queryParams.musicName = row.musicName
      this.queryParams.musicIndex = row.musicIndex
      this.queryParams.musicMaker = row.musicMaker
      this.queryParams.id = row.musicId
      console.log('====================================');
        console.log(this.queryParams);
        console.log('====================================');

    },
    /** 提交按钮 */
    submitForm() {
       // 关闭弹窗
  // this.dialogVisible = false;
  // 校验表单
  this.$refs["queryParams"].validate(valid => {
    if (valid) {
          if (this.queryParams.id != null) {
            updateMusicinfo(this.queryParams).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.opens = false;
             
              this.getList();
            });
          }
  }
  });
},
    getMusicList(bookId) {
      getMusics(bookId).then(response => {
        this.loading = false
        console.log(response)
        this.musicInfoList = response.data;
       
      })
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "给书籍添加乐曲";
      this.getList()
    },
    getList() {
      listMusicinfo(this.queryParams).then(res => {
        this.musicChooseList = res.rows
        this.total = res.total
        
      })
    },
    handleQuery() {

    },
    resetQuery() {

    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        musicName: null,
      };
      this.resetForm("form");
    },
    addToBookAction() {
      const ids = this.ids;
      console.log(ids)
      addMusic(ids, this.bookId).then(res => {
        if (res.code == 200) {
          this.open = false
          this.getMusicList(this.bookId)
        }
      })
    },
    removeMusic(row) {
      if (row.id === undefined) {
        return
      }
      removeMusic(row.id).then(res => {
        if (res.code == 200) {
          this.getMusicList(this.bookId)
        }
      })
    }
  }
}
</script>

<style scoped></style>
